<?php
	
	/**	
		* controller home		
		* giao dien nguoi dung
	**/

	class Home extends CI_Controller
	{

		// dinh nghia vai gia tri
		var $template;
		var $template_absolute_path;

		public function __construct()
		{

			parent::__construct();			

			// dinh nghia giao dien, duong dan giao dien
			$this->template = 'thegioihandmade/';
			$this->template_absolute_path = 'template/reponsive/';

			//enable cache
			//$this->output->cache(1);

			//
			$this->load->library('session');

			//

			$this->load->model('Category');

			$this->load->model('Product');

			$this->load->model('Ad');

			$this->load->model('News');

			$this->load->model('Page');

			$this->load->model('NewsCategory');

			$this->load->model('Setting');

			//$this->load->model('Counter');

			$this->load->model('Support');		

			$this->load->model('Messages');

			//increase counter

			//$this->Counter->increaseCounter();

			//quick fix patch

			//remove search session

			$this->session->unset_userdata('search_products_results');
		}

		/*
			hien thi trang chu
			show 9 san pham moi nhat
		*/
		public function index()
		{		

			// No enable cache for this page
			//$this->output->cache(120);	

			//display header

			$this->display_header();

			//left sidebar					

			//$this->left_sidebar();

			//center

			$this->displayHomePage();

			//right sidebar

			//$this->right_sidebar();

			//footer
			$this->footer();

		}



		//center homepage

		function displayHomePage()
		{						
			// hien thi quang cao
			$data['homebanner'] = $this->Ad->getHomeAds();

			//$data['new_products'] = $this->Product->getNewProducts();

			////////////////// hien thi [9] san pham moi ////////////////////////
			$data['new_products'] = $this->Product->getNewProducts();
			/////////////////////////////////////////////////////////////////////			

			////////////////// hinh anh slide ////////////////////////
			
			// $data['searchform'] = $this->displaySearchForm();
			
			$this->load->view($this->template.'center',$data);
		}


		/*
			* header website
			* 			
		*/
		function display_header($title = false)
		{
			$data['template'] = $this->template;
			$data['template_path'] = $this->template_absolute_path;

			$data['company'] = $this->Setting->getCompany();

			// show title & description of page
			$title_page = $this->session->userdata('title_page');
			$description_page = $this->session->userdata('description_page');

			if( !empty($title_page) && !empty($description_page) )
			{
				$data['title_page'] = $title_page;
				$data['description_page'] = $description_page;

				$this->session->unset_userdata('title_page');
				$this->session->unset_userdata('description_page');
			}
			else
			{
				$data['title_page'] = 'THẾ GIỚI HANDMADE';
				$data['description_page'] = $data['company']['description'];
			}

			////////////////////// hien thi danh muc //////////////////////
			$categories = $this->Category->getAllCategories();

			$data['order_categories'] = $this->displayCategory($categories);
			////////////////////////////////////////////////////////////////


			////////////////////// hien thi quang cao //////////////////////
			$data['homebanner'] = $this->Ad->getAllAds();
			////////////////////////////////////////////////////////////////

			$data['keyword'] = '';

			$data['title'] 	 = '';

			$data['header_links'] = array(

					'1'	=> array('div' => 'root normal','title' => site_url(),'href' => base_url()),

					'2'	=> array('div' => 'root normal','title' => 'Trang chủ','href' => base_url()),

					'3'	=> array('div' => 'root normal','title' => 'Giới Thiệu','href' => site_url('home/page/introduction')),

					'4'	=> array('div' => 'root normal','title' => 'Bảo Hành','href' => site_url('home/page/warranty')),

					'5'	=> array('div' => 'root normal','title' => 'Sửa chữa','href' => site_url('home/product_category/34')),	//fix patch for category

					'6'	=> array('div' => 'root normal','title' => 'Liên Hệ','href' => site_url('home/page/contact')),

				);

			if($title == 'introduction')

				$data['header_links']['3']['div'] = 'root current';

			else if($title == 'warranty')

				$data['header_links']['4']['div'] = 'root current';

			else if($title == 'contact')

				$data['header_links']['6']['div'] = 'root current';


			$this->load->view($this->template.'header',$data);
		}



		//left side bar display

		function left_sidebar($id = false)
		{

			//display categories for left side bar

			$categories = $this->Category->getAllCategories($id);

			$data['order_categories'] = $this->displayCategory($categories);



			//advertisement left

			$data['ad_left'] = $this->Ad->getLeftAds();		

			//stat

			//$data['counter'] = $this->Counter->getCounter();	

			$this->load->view('left',$data);
		}



		//right side bar display

		function right_sidebar()
		{
			//support

			$data['support'] = $this->Support->getAll();

			//news 

			$data['news'] = $this->News->getAll(5);

			//new categories

			$data['news_categories'] = $this->NewsCategory->getAll(5);

			//right ads

			$data['ad_right'] = $this->Ad->getRightAds();			



			$this->load->view('right',$data);
		}



		//footer display

		function footer()
		{

			// $data['footer_links'] = array(

			// 		'1' => array('href' => site_url(),'title' => 'Đức Ngọc O2'),

			// 		'2' => array('href' => site_url(), 'title' => 'Trang Chủ'),

			// 		'3' => array('href' => site_url('home/page/introduction'), 'title' => 'Giới Thiệu'),

			// 		'4' => array('href' => site_url('home/product_category/34'), 'title' => 'Sửa Chữa'),

			// 		'5' => array('href' => site_url('home/page/map'), 'title' => 'Bản Đồ'),

			// 		'6' => array('href' => site_url('home/page/contact'), 'title' => 'Liên Hệ'),

			// 	);

			$data['setting'] = $this->Setting->getCompany();			

			$this->load->view($this->template.'footer',$data);
		}



		/***********************************************************

		 * Common functions

		 * display categories in parent and children

		***********************************************************/



		//search function

		public function displaySearchForm()
		{

			return 	'<div class="center_search_form">

					<div class="L">

					<div class="R">

						<form name="center_search_form" action="'.site_url('home/search').'" method="post">

							<b lang="vi">Tìm kiếm</b> : 

							<input title="Từ khóa tìm kiếm" type="text" class="form_control keyword" name="keyword" value="" />&nbsp;&nbsp; 

							<span lang="vi">Giá từ</span> : 

							<input title="Giá nhỏ nhất" type="text" class="form_control min_price" name="price" value="" />&nbsp;&nbsp; ~

							<span lang="vi">Đến</span> : 

							<input title="Giá lớn nhất" type="text" class="form_control max_price" name="price_to" value="" />&nbsp; 

							<input title="Tìm kiếm" type="submit" class="form_button" value="Tìm kiếm" />

						</form>

					</div>

					</div>

					</div>';

		}



		public function search()

		{

			//pagination

			$this->load->library('pagination');

			$config['base_url'] = site_url('home/search_result/');

			$config['uri_segment'] = 3;

			$config['total_rows'] = 200;

			$config['per_page'] = 10; 

			$config['full_tag_open'] = '<div class="page_div">';

			$config['full_tag_close'] = '</div>';

			$this->pagination->initialize($config); 

			$data['pagination'] = $this->pagination->create_links();







			$keyword = $this->input->post('keyword');

			$price = $this->input->post('price');

			$price_to = $this->input->post('price_to');



			$data['search'] = array(

					'keyword' => (isset($keyword) && !empty($keyword)) ? $keyword : 'all',

					'price' => (isset($price) && !empty($price)) ? $price : '0',

					'price_to' => (isset($price_to) && !empty($price_to)) ? $price_to : '0',

				);



			$data['search_products'] = $this->Product->search($data);			



			$this->session->set_userdata('search_products_results',$data);



			//display product

			//display header

			$this->display_header();

			//left sidebar

			$this->left_sidebar();

			//center

			$data['search_form'] = $this->displaySearchForm();

			$this->load->view('search_result_page',$data);

			//right sidebar

			$this->right_sidebar();

			//footer

			$this->footer();

			//display product

		}



		public function search_result($page = false)

		{

			$search_products_results = $this->session->userdata('search_products_results');

			if(!isset($search_products_results) && empty($search_products_results))

			{

				redirect('home/index','refresh');

			}

			else

			{

				//pagination

				$this->load->library('pagination');

				$config['base_url'] = site_url('home/search_result/');

				$config['uri_segment'] = 3;

				$config['total_rows'] = 200;

				$config['per_page'] = 10; 

				$config['full_tag_open'] = '<div class="page_div">';

				$config['full_tag_close'] = '</div>';

				$this->pagination->initialize($config); 

				$data['pagination'] = $this->pagination->create_links();



				//display product

				//display header

				$this->display_header();

				//left sidebar

				$this->left_sidebar();

				//center

				$data['search_products'] = $this->Product->search($search_products_results,$page);

				$data['search_form'] = $this->displaySearchForm();

				$this->load->view('search_result_page',$data);

				//right sidebar

				$this->right_sidebar();

				//footer

				$this->footer();

				//display product

			}

		}



		public function page($title = false)
		{

			$this->output->cache(120);

			//display header

			$this->display_header($title);

			//left sidebar

			$this->left_sidebar();

			//center

			//show page content 

			if($title == 'warranty')
			{

				$data['policy'] = $this->Page->getPageContent('policy');

			}

			$data['page'] = $this->Page->getPageContent($title);



			if(empty($data['page']))

			{

				redirect('home/index','refresh');

			}



			$data['search_form'] = $this->displaySearchForm();

			$this->load->view('page',$data);

			//right sidebar

			$this->right_sidebar();

			//footer

			$this->footer();
		}



		public function new_detail($id = false)
		{
			$this->output->cache(120);

			//display header

			$this->display_header();

			//left sidebar

			$this->left_sidebar();

			//center

			//show page content 

			$data['new'] = $this->News->getNew($id);

			$data['news'] = $this->News->getAll(50);


			$data['current_page'] = site_url('new_detail/'.$id);

			if(empty($data['new']))

			{

				redirect('home/index','refresh');

			}



			$data['search_form'] = $this->displaySearchForm();

			$this->load->view('new_detail',$data);

			//right sidebar

			$this->right_sidebar();

			//footer

			$this->footer();

		}



		public function new_category($id = false,$page = false)
		{			

			if($id == false)

			{

				redirect('home/index','refresh');

			}

			//display header

			$this->display_header();

			//left sidebar

			$this->left_sidebar();

			//center

			//show page content 		

			$data['category'] = $this->NewsCategory->getCategory($id);

			$data['news'] = $this->NewsCategory->getNewsOfCategory($id,$page);



			if(empty($data['news']))

			{

				redirect('home/index','refresh');

			}



			$data['search_form'] = $this->displaySearchForm();



			$this->load->library('pagination');

			$config['base_url'] = site_url('home/new_category/'.$id.'/');

			$config['uri_segment'] = 4;

			$config['total_rows'] = 200;

			$config['per_page'] = 10; 			

			$config['full_tag_open'] = '<div class="page_div">Trang:&nbsp;';

			$config['full_tag_close'] = '</div>';	

			$this->pagination->initialize($config); 

			$data['pagination'] = $this->pagination->create_links();



			$this->load->view('new_category',$data);

			//right sidebar

			$this->right_sidebar();

			//footer

			$this->footer();

		}



		//products of category

		public function product_category($category_id = false,$page = false)
		{

			$this->load->library('pagination');

			$config['base_url'] = site_url('home/product_category/'.$category_id.'/');

			$config['total_rows'] = $this->Product->count_results($category_id);

			$config['per_page'] = 9; 

			$config['uri_segment'] = 4;

			$config['full_tag_open'] = '<div class="page_div">Trang:&nbsp;';

			$config['full_tag_close'] = '</div>';

			$this->pagination->initialize($config); 

			$data['pagination'] = $this->pagination->create_links();						

			//display header

			$this->display_header();

			//left sidebar

			//$this->left_sidebar($category_id);

			//center

			//show page content 

			$data['category_name'] = $this->Category->getCategory($category_id);

			$data['productOfCategory'] = $this->Category->showProductsOfCategory($category_id,$page);

			$count_data = count($data['productOfCategory']);

			

			if($count_data < 10)
			{

				$data['pagination'] = '';
			}



			if(empty($data['productOfCategory']))
			{

				redirect('home/index','refresh');
			}



			$data['search_form'] = $this->displaySearchForm();

			$this->load->view($this->template.'product_category',$data);

			//right sidebar

			//$this->right_sidebar();

			//footer

			$this->footer();

		}



		/*
			* Hien thi chi tiet san pahm
		*/

		public function product_detail($id = false)
		{

			//display header
			$this->display_header();

			//left sidebar
			//$this->left_sidebar();

			//center

			//show page content 			
			// $data['search_form'] = $this->displaySearchForm();

			// hien thi san pham, danh muc
			$data['product'] = $this->Product->getProduct($id);

			// retrieve list of products same category
			$data['products'] = $this->Product->getProductsSameCat($id);

			$data['category'] = $this->Category->getCategory($data['product']['category_id']);

			// set page title & description
			$this->session->set_userdata('title_page', $data['product']['name']);
			$this->session->set_userdata('description_page', $data['product']['name']);

			if(empty($data['product']))
			{
				redirect('home/index','refresh');
			}



			$this->load->view($this->template.'product_detail',$data);

			//right sidebar

			//$this->right_sidebar();

			//footer
			$this->footer();

		}



		function displayCategory($categories = false,$sub_category = false)
		{

			$category_display = '';


			foreach ($categories as $category) 
			{				

				if($category['sub_category'] == 0)	
				{

					$category_display .= '<h4><a href="'.site_url('home/product_category/'.$category['id']).'">'.$category['category'].'</a></h4>';

					$category_display .= '<ul>'.$this->displayChildCategory($categories,$category['id']).'</ul>';
				}					

			}

			return $category_display;
		}



		function displayChildCategory($categories = false, $sub_category = false)
		{

			$category_display = '';

			foreach ($categories as $category) 

			{				

				if($sub_category > 0)

				{

					if($category['sub_category'] == $sub_category)

					{

						$category_display .= '<li><a href="'.site_url('home/product_category/'.$category['id']).'"><span lang="vi">'.$category['category'].'</span></a></li>';

					}

				}

			}

			return $category_display;

		}



		/***********************************************************

		 * End common functions		 

		***********************************************************/

		// contact
		public function contact()
		{
			//display header
			$this->display_header();

			$submit = $this->input->post('submitBtn');
			if( !empty($submit) )
			{
				$data = array(
						'name'	=>	$this->input->post('name'),
						'email'	=>	$this->input->post('email'),
						'website'	=>	$this->input->post('website'),
						'title'	=>	$this->input->post('title'),
						'message'	=>	$this->input->post('message'),
					);

				$this->Messages->addNewMsg($data);
			}

			$this->load->view($this->template.'contact');

			// footer
			$this->footer();
		}

		// event
		public function event()
		{
			//display header
			$this->display_header();

			$data['event'] = $this->Page->getPage(6);
			$this->load->view($this->template.'event', $data);

			// footer
			$this->footer();
		}

	}

